package io.sage.resolver;

import io.sage.entity.UserEntity;
import io.sage.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;
import io.sage.annotation.LoginUser;
import io.sage.interceptor.AuthorizationInterceptor;
import io.sage.modules.employer.entity.Employer;
import io.sage.modules.employer.service.EmployerService;

/**
 * 有@LoginUser注解的方法参数，注入当前登录用户
 */
@Component
public class LoginUserHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver {
    @Autowired
    private EmployerService studentService;
    @Autowired
    private UserService userService;

    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        return(parameter.getParameterType().isAssignableFrom(Employer.class)||parameter.getParameterType().isAssignableFrom(UserEntity.class)) && parameter.hasParameterAnnotation(LoginUser.class);
    }

    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer container,
                                  NativeWebRequest request, WebDataBinderFactory factory) throws Exception {
        //获取用户ID
        Object object = request.getAttribute(AuthorizationInterceptor.USER_KEY, RequestAttributes.SCOPE_REQUEST);
        if(object == null){
            return null;
        }

        //获取用户信息
        Object student = studentService.selectByPrimaryKey((Integer) object);
        if(student==null){
            student=userService.queryById((Integer) object);
        }
        return student;
    }
}
